package cn.edu.lingnan.translation.controller;


import cn.edu.lingnan.translation.entity.Admin;
import cn.edu.lingnan.translation.entity.Menu;
import cn.edu.lingnan.translation.entity.Role;
import cn.edu.lingnan.translation.entity.User;
import cn.edu.lingnan.translation.entity.vo.RoleQueryVo;
import cn.edu.lingnan.translation.service.RoleService;
import cn.edu.lingnan.translation.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Bao
 * @since 2020-10-15
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * @Description 获取角色分页列表
     * @Param [page, limit, roleName]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/15 14:30
     */
    @ApiOperation(value = "获取角色分页列表")
    @PostMapping("/{page}/{limit}")
    public R getRolePage(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        // 分页
        Page<Role> rolePage = new Page<>(page, limit);
        // 条件查询
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        IPage<Role> iPage = roleService.page(rolePage, wrapper);
        return R.ok().data("roles", iPage.getRecords()).data("total", iPage.getTotal());
    }

    /**
     * @Description 获取所有角色列表
     * @Param []
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/18 16:08
     */
    @ApiOperation(value = "获取所有角色列表")
    @GetMapping("/getAllRole")
    public R getAllRole() {
        List<Role> roleList = roleService.list(null);
        return R.ok().data("roleList", roleList);
    }

    /**
     * @Description 通过角色名获取角色信息
     * @Param [roleName]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/18 1:40
     */
    @ApiOperation(value = "通过角色名获取角色信息")
    @GetMapping("/roleName/{roleName}")
    public R getRoleByName(
            @ApiParam(name = "roleName", value = "角色名", required = false)
            @PathVariable String roleName) {
        Page<Role> page = new Page<>(1, 100);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(roleName)) {
            wrapper.like("role_name", roleName);
        }
        IPage<Role> roles = roleService.page(page, wrapper);
        return R.ok().data("roles", roles.getRecords());
    }

    /**
     * @Description 通过id获取角色信息
     * @Param [id]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/18 2:10
     */
    @ApiOperation(value = "通过id获取角色信息")
    @GetMapping("/{id}")
    public R addRole(@PathVariable String id) {
        Role role = roleService.getById(id);
        return R.ok().data("role", role);
    }

    /**
     * @Description 角色添加
     * @Param [role]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/18 1:43
     */
    @ApiOperation(value = "角色添加")
    @PostMapping("/add")
    public R addRole(@RequestBody Role role) {
        roleService.save(role);
        return R.ok();
    }

    /**
     * @Description 角色删除
     * @Param [id]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/18 1:41
     */
    @ApiOperation(value = "角色删除")
    @DeleteMapping("/{id}")
    public R deleteRole(@PathVariable String id) {
        roleService.removeById(id);
        return R.ok();
    }

    /**
     * @Description 角色更新
     * @Param [role]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/18 1:45
     */
    @ApiOperation(value = "角色更新")
    @PostMapping("/save")
    public R deleteRole(@RequestBody Role role) {
        roleService.updateById(role);
        return R.ok();
    }
}

